package com.gongpingjialoan.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.DomainActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.bean.AppData;
import com.gongpingjialoan.data.CarListFilterData;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.imageload.ImageLoad;
import com.gongpingjialoan.utility.Utils;

import java.util.List;

/**
 * Created by Administrator on 2016/10/19.
 */
public class MoreDomainView extends FrameLayout implements View.OnClickListener{

    private LinearLayout mains;

    private TextView allDomainTextView;

    private LayoutInflater mLayoutInflater;

    private Activity activity;

    private List<AppData.DataEntity.PlatCarSourceCountEntity.ValueEntity> values;

    public MoreDomainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        activity = (Activity) getContext();
        mLayoutInflater = LayoutInflater.from(getContext());
        mLayoutInflater.inflate(R.layout.layout_more_domain, this, true);
        mains = (LinearLayout) findViewById(R.id.domains);
        allDomainTextView = (TextView) findViewById(R.id.all_domain);
        allDomainTextView.setOnClickListener(this);
        if (GPJApplication.getInstance().mAppData != null && "success".equals(GPJApplication.getInstance().mAppData.getStatus())) {
            try {
                String  nums = GPJApplication.getInstance().mAppData.getData().getBuy_plat_count().getValue() + "";
                String msg = "还有" + nums + "个渠道车源供您选择>";
                allDomainTextView.setText(Utils.changeColor(getContext() , msg , 2 , 2+nums.length() ,R.color.discount_price_color));
                values = GPJApplication.getInstance().mAppData.getData().getPlat_car_source_count().getValue();
                int size = values.size();
                for (int i = 0; i < size; i++) {
                    View view = mLayoutInflater.inflate(R.layout.domain_hot_item, null);
                    ImageView networkImageView = (ImageView) view.findViewById(R.id.img);
                    TextView numTextView = (TextView) view.findViewById(R.id.nums);
                    numTextView.setText(values.get(i).getCount() + "");
                    ImageLoad.LoadImage(networkImageView, values.get(i).getLogo(), R.drawable.trans, R.drawable.trans,activity);
                    final int finalI = i;
                    view.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            CarListFilterData.getInstance().clear();
                            CarListFilterData.getInstance().setCar_domain(values.get(finalI).getDomain());
                            CarListFilterData.getInstance().setShow_domain(values.get(finalI).getName());
                            CarListFilterData.getInstance().setIsCleanData(true);
                            activity.finish();
                            MainActivity.main.gotocheshi();
                        }
                    });
                    mains.addView(view);
                }
            } catch (Exception e) {
                setVisibility(View.GONE);
            }
        } else {
            setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View v) {
        if(v == allDomainTextView){
            Intent intent = new Intent();
            intent.putExtra("domainString" , "");
            intent.setClass(activity, DomainActivity.class);
            MainActivity.main.startActivityForResult(intent, MainActivity.main.GETMORE_DOAMIN);
            activity.finish();
        }
    }
}
